﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using TierService;
using OrderMgr.SysSetting;
using OrderMgr.OrderInfo;
using OrderMgr.CustomerInfo;
using System.Collections;
using TierApplication;

namespace OrderMgr.OrderInfo
{
    public partial class frmLoanAudit : TierService.frmTemplate
    {

        int iOrderNum = 0;
        int iProductCount = 0;
        decimal dSumMoney = 0;

        public frmLoanAudit()
        {
            InitializeComponent();
            this.Text = "产品销售发货单";
        }

        private void initForm()
        {
            Utility.DataToLookupComboxEx(txtDepotID, C_DMB.GetListByType(C_DMB.DType.DepotType.ToString()), "CodeID", "CodeName");
            Utility.DataToLookupComboxEx(txtOrderUserID, (new P_User()).GetList(""), "UserID", "UserName");
            ckbOrderDate.Checked = true;
            txtOrderDateStart.DateTime = DateTime.Now.AddDays(-7);
            txtOrderDateEnd.DateTime = DateTime.Now.Date;
        }

        private void frmSaleAudit_Load(object sender, EventArgs e)
        {
            initForm();

        }

        private void btnSearch_Click(object sender, EventArgs e)
        {
            string ls_where = "";

            ls_where = " and a.OrderType ='" + (int)Utility.OrderType.Loan + "'";
            string sOrderStatus = (string)txtOrderStatus.EditValue;
            if (sOrderStatus == "1")
            {
                ls_where = ls_where + " and a.OrderStatus ='1'";
            }
            if ((sOrderStatus != "0") && (sOrderStatus != "1"))
            {
                ls_where = ls_where + " and a.OrderStatus >='2'";
            }
            //if ((txtCustName.Text.Trim() != "") && (ckbOrderID.Checked))
            //    ls_where = ls_where + " and CustName  like '%" + txtCustName.Text + "%'";

            if ((txtOrderID.Text != "") && (ckbOrderID.Checked))
                ls_where = ls_where + " and a.OrderID like '%" + txtOrderID.Text + "%'";

            if ((txtDepotID.Text.Trim() != "") && (ckbDepotID.Checked))
                ls_where = ls_where + " and a.DepotID = '" + (string)txtDepotID.EditValue + "'";

            if ((txtOrderUserID.Text.Trim() != "") && (ckbOrderUserID.Checked))
                ls_where = ls_where + " and a.OrderUserID = '" + (string)txtOrderUserID.EditValue + "'";

            if ((ckbOrderDate.Checked) && (txtOrderDateStart.Text != "") && (txtOrderDateEnd.Text != ""))
            {
                ls_where = ls_where + " and Convert(varchar(10),a.orderdate,120) >='" + txtOrderDateStart.DateTime.ToString("yyyy-MM-dd") + "'";
                ls_where = ls_where + " and Convert(varchar(10),a.orderdate,120) <='" + txtOrderDateEnd.DateTime.ToString("yyyy-MM-dd") + "'";
            }

            if ((txtCustID.Text.Trim() != "") && (ckbCustID.Checked))
                ls_where = ls_where + " and a.CustID = '" + txtCustID.Text + "'";

            ceAll.Checked = false;
            T_Order mOrder = new T_Order();
            DataSet dsCustomer = mOrder.GetOrderList(ls_where);
            gcOrder.DataSource = dsCustomer.Tables[0];
        }

        private void btnExportExcel_Click(object sender, EventArgs e)
        {
            TierService.Utility.DataExportToExcel(gvOrder, "订单信息");
        }



        private void gvOrder_CustomDrawRowIndicator(object sender, DevExpress.XtraGrid.Views.Grid.RowIndicatorCustomDrawEventArgs e)
        {
            e.Appearance.TextOptions.HAlignment = DevExpress.Utils.HorzAlignment.Far;
            if (e.Info.IsRowIndicator)
            {
                if (e.RowHandle >= 0)
                {
                    e.Info.DisplayText = (e.RowHandle + 1).ToString();
                }
                else if (e.RowHandle < 0 && e.RowHandle > -1000)
                {
                    e.Info.Appearance.BackColor = System.Drawing.Color.AntiqueWhite;
                    e.Info.DisplayText = "G" + e.RowHandle.ToString();
                }
            }
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            frmSaleAuditDetail fDetail = new frmSaleAuditDetail();
            fDetail.OpMode = TierService.Utility.OperateMode.Insert;
            fDetail._OrderStatus = Utility.OrderStatus.S0Input;
            fDetail._OrderType = Utility.OrderType.Sale;
            fDetail.ShowDialog();

            btnSearch_Click(null, null);
        }

        private void btnModify_Click(object sender, EventArgs e)
        {
            //if (gvOrder.RowCount < 1) return;
            //DataRow dr = gvOrder.GetDataRow(gvOrder.FocusedRowHandle);
            //string sID = dr["OrderID"].ToString();

            //frmSaleShipDetail fDetail = new frmSaleShipDetail();
            //fDetail.OpMode = TierService.Utility.OperateMode.Update;
            //fDetail._AuditStatus = Utility.OrderStatus.Input;
            //fDetail._OrderType = Utility.OrderType.Sale;
            //fDetail._OrderID = sID;
            //if (fDetail.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            //{
            //    btnSearch_Click(null, null);
            //}

            string sOrderIDs = "";
            ArrayList alOrderID = new ArrayList(5000);

            for (int i = 0; i < gvOrder.DataRowCount; i++)
            {
                DataRow dr = gvOrder.GetDataRow(i);
                if (dr["IsChecked"] == "1")
                {
                    string sOrderID = dr["OrderID"].ToString();
                    alOrderID.Add(sOrderID);
                }
            }

            //frmSaleShipInfo fDetail = new frmSaleShipInfo();
            //fDetail.OpMode = Utility.OperateMode.Insert;
            //fDetail._alOrderID = alOrderID;
            //fDetail.iOrderNum = iOrderNum;
            //fDetail.iProductCount = iProductCount;
            //fDetail.dSumMoney = dSumMoney;
            //if (fDetail.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            //{
            //    btnSearch_Click(null, null);
            //}
        }

        private void btnQuery_Click(object sender, EventArgs e)
        {
            if (gvOrder.RowCount < 1) return;
            DataRow dr = gvOrder.GetDataRow(gvOrder.FocusedRowHandle);
            string sID = dr["OrderID"].ToString();

            frmSalesDetail fDetail = new frmSalesDetail();
            fDetail.OpMode = TierService.Utility.OperateMode.Query;
            //fDetail._OrderStatus = Utility.OrderStatus.S0Input;
            fDetail._OrderType = Utility.OrderType.Loan;
            fDetail._OrderID = sID;
            fDetail.ShowDialog();
        }

        private void btnAudit_Click(object sender, EventArgs e)
        {
            if (gvOrder.RowCount < 1) return;
            DataRow dr = gvOrder.GetDataRow(gvOrder.FocusedRowHandle);
            string sID = dr["OrderID"].ToString();

            T_Order mOrder = new T_Order();
            mOrder.GetModel(sID);
            if (mOrder.OrderStatus == "2")
            {
                MessageBox.Show("此单据已经确认，不能进行修改!");
                return;
            }

            frmSalesDetail fDetail = new frmSalesDetail();
            fDetail.OpMode = TierService.Utility.OperateMode.Update;
            fDetail._AuditStatus = Utility.OrderStatus.S1Auditing;
            fDetail._OrderType = Utility.OrderType.Loan;
            fDetail._OrderID = sID;
            if (fDetail.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {

            }

            btnSearch_Click(null, null);

            //if (gvOrder.RowCount < 1) return;
            //if (MessageBox.Show("是否要审核选择单据？", "确认", MessageBoxButtons.OKCancel) == DialogResult.Cancel)
            //    return;
          
            //for (int i = 0; i < gvOrder.DataRowCount; i++)
            //{
            //    DataRow dr = gvOrder.GetDataRow(i);
            //    string sOrderID = dr["OrderID"].ToString();
            //    if (dr["IsChecked"] == "1")
            //    {
            //        T_Order mOrder = new T_Order();
            //        mOrder.Audit(sOrderID, Utility.gUserID);
            //    }
            //}

            //btnSearch_Click(null, null);

        }

        private void gvOrder_DoubleClick(object sender, EventArgs e)
        {
            btnQuery_Click(null, null);
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            if (gvOrder.RowCount < 1) return;
            if (MessageBox.Show("是否要删除此订单记录，删除后将无法恢复!", "确认", MessageBoxButtons.OKCancel) == DialogResult.Cancel)
                return;
            DataRow dr = gvOrder.GetDataRow(gvOrder.FocusedRowHandle);
            string sID = dr["OrderID"].ToString();

            T_Order mOrder = new T_Order();
            mOrder.Delete(sID);

            btnSearch_Click(null, null);
        }


        private void btnClose_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void txtCustID_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            frmCustomer fDetail = new frmCustomer();
            fDetail.gFuncType = Utility.FuncType.Select;
            if (fDetail.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                txtCustID.Text = fDetail._CustID;
            }
        }

        private void ceAll_CheckedChanged(object sender, EventArgs e)
        {
            for (int i = 0; i < gvOrder.DataRowCount; i++)
            {
                DataRow dr = gvOrder.GetDataRow(i);
                dr["IsChecked"] = ceAll.Checked ? "1" : "0";
            }


            iProductCount = 0;
            iOrderNum = 0;
            dSumMoney = 0;
            for (int i = 0; i < gvOrder.DataRowCount; i++)
            {
                DataRow dr = gvOrder.GetDataRow(i);
                if (dr["IsChecked"] == "1")
                {
                    iOrderNum = iOrderNum + 1;
                    iProductCount = iProductCount + Convert.ToInt32(Math.Floor(decimal.Parse(dr["OrderActQuantity"].ToString())));
                    dSumMoney = dSumMoney + decimal.Parse(dr["OrderActMoney"].ToString());
                }
            }
            lblProductCOunt.Text = "选择单据数量：" + iOrderNum.ToString() + ",商品总数量：" + iProductCount.ToString() + ",总金额：" + dSumMoney.ToString();

        }

        private void gvOrder_Click(object sender, EventArgs e)
        {
                  }

        private void gvOrder_RowCellClick(object sender, DevExpress.XtraGrid.Views.Grid.RowCellClickEventArgs e)
        {
            if (gvOrder.RowCount < 1) return;
            DataRow dr = gvOrder.GetDataRow(gvOrder.FocusedRowHandle);
            dr["IsChecked"] = (dr["IsChecked"] == "1") ? "0" : "1";

            iProductCount = 0;
            iOrderNum = 0;
            dSumMoney = 0;
            for (int i = 0; i < gvOrder.DataRowCount; i++)
            {
                DataRow drOrder = gvOrder.GetDataRow(i);
                if (drOrder["IsChecked"] == "1")
                {
                    iProductCount = iProductCount + 1;
                    iOrderNum = iOrderNum + Convert.ToInt16(Math.Floor(double.Parse(dr["OrderActQuantity"].ToString())));
                    dSumMoney = dSumMoney + decimal.Parse(drOrder["OrderActMoney"].ToString());
                }
            }
            lblProductCOunt.Text = "选择单据数量：" + iProductCount.ToString() + ",商品总数量：" + iOrderNum.ToString() + ",总金额：" + dSumMoney.ToString();

        }

        private void btnDeAudit_Click(object sender, EventArgs e)
        {


            if (MessageBox.Show("是否要退回此单据?", "确认", MessageBoxButtons.OKCancel) == DialogResult.Cancel)
                return;

            if (gvOrder.RowCount < 1) return;
            DataRow dr = gvOrder.GetDataRow(gvOrder.FocusedRowHandle);
            string sOrderID = dr["OrderID"].ToString();

            T_Order mOrder = new T_Order();
            mOrder.DeAudit(sOrderID, Utility.gUserID, "");
            MessageBox.Show("审核退回成功");
            btnSearch_Click(null, null);
            
        }
    }

}